import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/alert/alert.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Future<int> showDiscountAlert(BuildContext context) {
  String discount = '';
  return showAlert<int>(
    context,
    content: _DiscountAlert(
      onChanged: (value) {
        discount = value;
      },
    ),
    onCancel: (context) {
      Navigator.pop(context);
    },
    onConfirm: (context) {
      if (discount == '') {
        toast('请输入折扣率');
        return;
      }

      Navigator.pop(context, int.parse(discount));
    },
  );
}

class _DiscountAlert extends StatefulWidget {
  final ValueChanged<String> onChanged;

  const _DiscountAlert({
    Key key,
    this.onChanged,
  }) : super(key: key);

  @override
  __DiscountAlertState createState() => __DiscountAlertState();
}

class __DiscountAlertState extends State<_DiscountAlert> {
  OutlineInputBorder _inputBorder = OutlineInputBorder(
    borderSide: BorderSide(
      color: ColorConstant.e5e5,
      width: 1.px * 2,
    ),
    borderRadius: BorderRadius.circular(
      10.w,
    ),
  );

  TextEditingController _controller;

  @override
  void initState() {
    super.initState();

    _controller = TextEditingController();
    _controller.addListener(_handleChange);
  }

  @override
  void dispose() {
    _controller.removeListener(_handleChange);
    _controller.dispose();
    _controller = null;
    super.dispose();
  }

  _handleChange() {
    if (_controller.value.text.length > 2) {
      var text = _controller.text.substring(0, 2);
      _controller.text = text;
      _controller.selection = TextSelection.collapsed(offset: 2);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(
        horizontal: 30.w,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(
              top: ScreenUtil.instance.setWidth(47.5),
              bottom: ScreenUtil.instance.setWidth(27.5),
            ),
            child: Text(
              '批量设置折扣率',
              style: TextStyleConstant().normal_1_28,
            ),
          ),
          Stack(
            alignment: Alignment.center,
            children: <Widget>[
              TextFormField(
                keyboardAppearance: Brightness.light,
                controller: _controller,
                onChanged: widget.onChanged,
                textAlign: TextAlign.center,
                decoration: InputDecoration(
                  enabledBorder: _inputBorder,
                  focusedBorder: _inputBorder,
                  hintText: '例：打九折，填写90',
                  hintStyle: TextStyleConstant().normal_4_28,
                  contentPadding: EdgeInsets.symmetric(
                    vertical: 22.w,
                    horizontal: 60.w,
                  ),
                ),
                style: TextStyleConstant().normal_1_28,
                keyboardType: TextInputType.number,
                inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
              ),
              Positioned(
                top: 0,
                right: 0,
                bottom: 0,
                child: Container(
                  width: 60.w,
                  alignment: Alignment.center,
                  child: Text(
                    '%',
                    style: TextStyle(
                      fontSize: 32.sp,
                      color: ColorConstant.ff33,
                      height: 1,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
